package sender
import java.sql.*;
import groovy.sql.Sql
import java.lang.reflect.Array;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
class Main {
	
	static main(args) {
		
		def mysqlservice ="127.0.0.1:3306/test"
		def mysqlusername="root"
		def mysqlpsw="aaaaaaaa"
		//������ݿ�
		def sql = Sql.newInstance("jdbc:mysql://${mysqlservice}", mysqlusername,mysqlpsw, "com.mysql.jdbc.Driver")
		
		checkTable sql
		
		
		//��ȡ���еķ���Email���ʺ�
		def senderList=sql.rows("select * from EmailAccout")
		//println senderList
		
		if(senderList.size()<1){
			println "û�з���Email���˻�"
			return
		}
		
		
		
		def sendMessages=sql.rows("select * from sendEmail limit 0 ,100")
		//while��ʼ  ���ݱ�������
		if(sendMessages.size()>0){
			sendMessages.each {
				
				sendEmail(senderList[2],it)
				
				//����¼���浽ָ����
				sql.execute("INSERT INTO `test`.`sendemailok` " +
						"(`sendto`, `username`, `subject`, `content`)" +
						" VALUES (?, ?, ?, ?)",[it.sendto,it.username,it.subject,it.content])
				//ɾ��ǰ��¼
				sql.execute("delete from sendEmail where idsendEmail = ?",[it.idsendEmail])
				
			}
			sendMessages=sql.rows("select * from sendEmail limit 0 ,100")
		}
		//��ȡ��Ҫ���͵����ݣ�100����
		//�����ʼ���Email�ʻ�����Ҫ����ʹ�ã����������
		//������Ϣ
		//���ɹ��ļ�¼ת�Ƶ�����һ�ű�
		//end while
		
		//�ر���ݿ�
		sql.close()
	}
	
	
	
	def static sendEmail(def username ,def message){
		// ����һ��email
		MultiPartEmail multipartemail = new MultiPartEmail();
		if(username['type']=="qq"){
			multipartemail.setHostName("smtp.qq.com"); // �����ʼ���������ַ
		}else if(username['type']=="163"){
			multipartemail.setHostName("smtp.163.com"); // �����ʼ���������ַ
		}else if(username['type']=="gmail"){
			multipartemail.setSSL(Boolean.TRUE); // �趨�Ƿ�ʹ��SSL
			multipartemail.setSslSmtpPort("465"); // �趨SSL�˿�
			multipartemail.setHostName("smtp.gmail.com"); // �����ʼ���������ַ
			
		}else{
			multipartemail.setHostName("smtp.qq.com"); // �����ʼ���������ַ
			
		}
		multipartemail.addTo(message['sendto'], message['username']);// �Է�
		multipartemail.setFrom(username['user'], username['name']);// �ҷ�
		multipartemail.setAuthentication(username['user'], username['psw']);
		multipartemail.setSubject(message['subject']);// ����
		multipartemail.setContent(message['content'], "text/html;charset=GBK")// html
		// �����ʼ�
		multipartemail.send();
	}
	
	
	
	/**
	 * ����Ҫ�ı�
	 * @param sql
	 * @return
	 */
	def static checkTable(def sql){
		//����ǰ��������ļ�,��������û���
		//select   1   from   information_schema.tables   where   table_name   =   'EmailAccout ';
		
		if( 0==sql.rows("select   1   from   information_schema.tables   where   table_name   =   'EmailAccout'").size()){
			sql.execute """
			CREATE  TABLE `EmailAccout` (
				  `idEmailAccout` INT NOT NULL AUTO_INCREMENT ,
				  `user` VARCHAR(45) NULL ,
				  `psw` VARCHAR(45) NULL ,
				  `name` VARCHAR(45) NULL ,
				  `type` VARCHAR(45) NULL COMMENT '163,qq,gmail' ,
				  PRIMARY KEY (`idEmailAccout`) )
				COMMENT = 'Email accout';
		"""
		}
		//������ݱ��Ƿ����
		if( 0==sql.rows("select   1   from   information_schema.tables   where   table_name   =   'sendEmail'").size()){
			sql.execute """
						CREATE  TABLE  `sendEmail` (
						  `idsendEmail` INT NOT NULL AUTO_INCREMENT ,
						  `sendto` VARCHAR(45) NULL ,
						  `username` VARCHAR(45) NULL ,
						  `subject` VARCHAR(500) NULL ,
						  `content` VARCHAR(5000) NULL ,
						  `attachment` VARCHAR(100) NULL COMMENT 'like this\n \'\nd:\\\\1.jpg&d:\\\\2jpg\n\'' ,
						  PRIMARY KEY (`idsendEmail`) )
						COMMENT = 'need send email';
			"""
		}
		//��� ���ͳɹ���¼�洢��
		if( 0==sql.rows("select   1   from   information_schema.tables   where   table_name   =   'sendEmailOk'").size()){
			sql.execute """
						 CREATE  TABLE  `sendEmailOk` (
						  `idsendEmail` INT NOT NULL AUTO_INCREMENT ,
						  `sendto` VARCHAR(45) NULL ,
						  `username` VARCHAR(45) NULL ,
						  `subject` VARCHAR(500) NULL ,
						  `content` VARCHAR(5000) NULL ,
						  `attachment` VARCHAR(100) NULL COMMENT 'like this\n \'\nd:\\\\1.jpg&d:\\\\2jpg\n\'' ,
						  PRIMARY KEY (`idsendEmail`) )
						COMMENT = 'need send email';
			"""
		}
	}
}
